﻿Imports Aricie.DNN.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Portals

Imports System.IO
Imports Aricie.Services
Imports System.Xml.Linq
Imports UpgradeSystem
Imports System.Linq
Imports Aricie.DNN.Diagnostics


Namespace Aricie.DNN.Modules.LuceneSearch.Business

    Public Class LuceneSearchController
        Implements IUpgradeable
        Implements IPortable

        Public Function UpgradeModule(ByVal Version As String) As String Implements DotNetNuke.Entities.Modules.IUpgradeable.UpgradeModule
            Dim toReturn As String = "Upgrade to " & Version & " successful"
            Try
                Dim UE As New Aricie.DNN.Services.UpgradeSystem.UpgradeEngine(Of LuceneSearchController)(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)
                UE.ResolveAndAddUpgrades()
                UE.MigrateSettingsTo(New Version(Version))

            Catch ex As Exception
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex)
                toReturn = "Error while upgrading to " & Version & " . See Event viewer for more information: " & ex.Message
            End Try

            Return toReturn
        End Function


        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements DotNetNuke.Entities.Modules.IPortable.ExportModule
            Dim pid As Integer = Aricie.DNN.Services.NukeHelper.GetPortalIdByModuleId(ModuleID)
            Dim export As New LuceneSearchExport
            export.PortalSettings = GetModuleSettings(Of LucenePortalSettings)(SettingsScope.PortalSettings, pid)
            export.ModuleSettings = GetModuleSettings(Of LuceneResultsSettings)(SettingsScope.ModuleSettings, ModuleID)
            export.ModuleTemplates = Global.Configuration.Templating.TemplateProvider.Usage.SerializableResource(New Global.Configuration.Templating.Scope.ModuleScope(pid, ModuleID)).AsSerializable()
            export.ProvidersSettings = LuceneSearchConfig.Instance

            Dim doc As System.Xml.XmlDocument = ReflectionHelper.Serialize(export, True)
            Return doc.OuterXml
        End Function

        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserID As Integer) Implements DotNetNuke.Entities.Modules.IPortable.ImportModule
            Dim pid As Integer = Aricie.DNN.Services.NukeHelper.GetPortalIdByModuleId(ModuleID)

            Dim import As LuceneSearchExport = ReflectionHelper.Deserialize(Of LuceneSearchExport)(Content)
            SetModuleSettings(Of LucenePortalSettings)(SettingsScope.PortalSettings, pid, import.PortalSettings)
            SetModuleSettings(Of LuceneResultsSettings)(SettingsScope.ModuleSettings, ModuleID, import.ModuleSettings)
            Global.Configuration.Templating.TemplateProvider.Usage.SerializableResource(New Global.Configuration.Templating.Scope.ModuleScope(pid, ModuleID)).FromSerializable(import.ModuleTemplates)
            LuceneSearchConfig.Save(import.ProvidersSettings)
        End Sub

    End Class



End Namespace
